package apibasej.shared.util;

import java.util.ArrayList;

public class UtilStrings {

	public static void main(String[] args) {
		
		System.out.println(">>>"+toJavaContantPattern("NomeDaClasse"));
		System.out.println(">>>"+toJavaContantPattern("nomeDaVar"));
		
	}
	
	
	public static String toJavaPattern(String str, boolean isEnt, boolean isPlural){
		StringBuilder sb = new StringBuilder(str.toLowerCase());
		if(isEnt) sb.setCharAt(0,Character.toUpperCase(sb.charAt(0)));
		for(int i=1;i<sb.length();i++){
			char atual = sb.charAt(i);
			if(atual=='_') {
				sb.setCharAt(i+1,Character.toUpperCase(sb.charAt(i+1)));
				sb.deleteCharAt(i);
			}
		}
		if(isPlural) sb.append('s');
		return sb.toString();
	}
	public static String toJavaContantPattern(String str){
		StringBuilder sb = new StringBuilder(str);
		for(int i=0;i<sb.length(); i++){
			if(i>0 && Character.isUpperCase(sb.charAt(i)) && !Character.isUpperCase(sb.charAt(i-1))){
				sb.insert(i, '_');
				i++;
			}
		}
		return sb.toString().toUpperCase();
	}
	
	public static String genRegexEndsWith(String strEnd, boolean ignoreCase){
		StringBuilder r = new StringBuilder();
		if(ignoreCase) r.append("(?i)");
		r.append(".*");
		r.append(strEnd);
		r.append("$");
		return r.toString();
	}
	
	public static String genRegex(ArrayList<String> patternsIgnore, ArrayList<String> patternsInclude, boolean ignoreCase){
		StringBuilder r = new StringBuilder();
		if(patternsIgnore!=null && patternsIgnore.size()>0){
			r.append("(?!");
			if(ignoreCase) r.append("(?i)");
			boolean appendOr = false;
			for(String p : patternsIgnore){
				if(appendOr) r.append('|');
				r.append(p);
				appendOr = true;
			}
			r.append(')');
		}
		if(patternsInclude==null || patternsInclude.size()==0){
			r.append(".*");
		}else{
			r.append('(');
			if(ignoreCase) r.append("(?i)");
			boolean appendOr = false;
			for(String p : patternsInclude){
				if(appendOr) r.append('|');
				r.append(p);
				appendOr = true;
			}
			r.append(')');
		}
		return r.toString();
		
	}
	
}
